import * as Cesium from 'cesium';

/**
 * https://blog.csdn.net/qq_26991807/article/details/121685935
 * @description 获取相机位置，姿态等
 * @param {*} viewer
 * @returns
 */
export const getcameraPosInfoFn = (viewer) => {
  // 获取 相机姿态信息
  var head = viewer.scene.camera.heading;
  var pitch = viewer.scene.camera.pitch;
  var roll = viewer.scene.camera.roll;
  var info = { head: head, pitch: pitch, roll: roll };
  // 获取位置 wgs84的地心坐标系，x,y坐标值以弧度来表示
  var position = viewer.scene.camera.positionCartographic; //with longitude and latitude expressed in radians and height in meters.
  //以下方式也可以获取相机位置只是返回的坐标系不一样
  // var position = viewer.scene.camera.position //cartesian3 空间直角坐标系
  // var ellipsoid = scene.globe.ellipsoid;
  // var position =ellipsoid.cartesianToCartographic(viewer.scene.camera.position)//
  // 弧度转经纬度
  var longitude = Cesium.Math.toDegrees(position.longitude).toFixed(6);
  var latitude = Cesium.Math.toDegrees(position.latitude).toFixed(6);
  var height = position.height;
  return { lng: longitude, lat: latitude, h: height, mat: info };
};

/**
 *@description 飞往指定的位置及指定的姿态展示
 */
export const flyToFn = (info) => {
  const {} = info;
  //将获取的姿态信息和位置信息给到camera即可
  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(
      121.638221,
      23.322379,
      3952.2551284496844
    ),
    orientation: {
      heading: 5.21762380092823,
      pitch: -0.34814053638414677, //Cesium.Math.toRadians(-35.0),
      roll: 0.00015736513513076034,
    },
  });
};

/**
 * @description 拾取坐标的几种方式
 * api https://mp.weixin.qq.com/s/2VCnkUpb8HjnmIUT1MhZGQ
 *
 */
export const curPosiFn = (viewer, options = {}) => {
  // 获取地表的坐标，延相机至屏幕坐标形成的射线--加载地形情况下
  const ray = viewer.camera.getPickRay(px);
  if (!ray) return null;
  const cartesian1 = viewer.scene.globe.pick(ray, viewer.scene);
  // 获取椭球面上的的坐标
  var ellipsoid = viewer.scene.globe.ellipsoid;
  const cartesian2 = viewer.scene.camera.pickEllipsoid(px, ellipsoid);
  // 获取场景中的坐标
  const cartesian3 = viewer.scene.pickPosition(px);
  // 获取模型的坐标，场景中的坐标
  const cartesian4 = viewer.scene.pickPosition(px);
  return {
    cartesian1,
    cartesian2,
    cartesian3,
    cartesian4,
  };
};
